package com.ccf.business.database.controller;

import cn.hutool.core.util.StrUtil;
import com.ccf.business.database.model.TableCache;
import com.ccf.business.database.service.ITableCacheService;
import com.common.model.pojo.Query;
import com.common.model.result.PageResult;
import com.common.model.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 创建表信息缓存
 *
 * @author ccf
 * @date 2022-01-11 17:12:35
 */
@Slf4j
@RestController
@RequestMapping("/tablecache")
@Api(tags = "创建表信息缓存")
public class TableCacheController {
    @Autowired
    private ITableCacheService tableCacheService;

    /**
     * 列表
     */
    @ApiOperation(value = "分页查询列表")
    @GetMapping
    public PageResult list(Query query) {
        return tableCacheService.findList(query);
    }


    /**
     * 查询
     */
    @ApiOperation(value = "查询")
    @GetMapping("/{id}")
    public Result getById(@PathVariable String id) {
        TableCache model = tableCacheService.getById(id);
        return Result.succeed(model, "查询成功");
    }

    /**
     * 新增or更新
     */
    @ApiOperation(value = "保存")
    @PostMapping
    public Result save(@RequestBody TableCache tableCache) {
        tableCacheService.saveOrUpdate(tableCache);
        return Result.succeed("保存成功");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id) {
        //批量删除
        if (StrUtil.contains(id, ",")){
            String[] ids = StrUtil.split(id, ",");
            tableCacheService.removeByIds(Arrays.stream(ids).collect(Collectors.toList()));
        } else {
            tableCacheService.removeById(id);
        }
        return Result.succeed("删除成功");
    }
}
